package com.liuheng.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuheng.manager.mapper.ProductDetailsMapper;
import com.liuheng.manager.mapper.ProductMapper;
import com.liuheng.manager.mapper.ProductSkuMapper;
import com.liuheng.manager.service.ProductService;
import com.liuheng.spzx.model.dto.product.ProductDto;
import com.liuheng.spzx.model.entity.product.Product;
import com.liuheng.spzx.model.entity.product.ProductDetails;
import com.liuheng.spzx.model.entity.product.ProductSku;
import com.liuheng.spzx.model.entity.product.ProductSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService{
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;
    @Override
    public Page findPage(int page, int limit, ProductDto productDto) {
        Page<Product> productPage = new Page<>(page,limit);
        List<Product> list=productMapper.findPage(productPage,productDto);
        productPage.setRecords(list);
        return productPage;
    }

    @Override
    public void saveProduct(Product product) {
        productMapper.insert(product);
        List<ProductSku> skuList = product.getProductSkuList();
        for (ProductSku productSku : skuList) {
            productSku.setSkuCode(product.getId()+"_"+888);
            productSku.setProductId(product.getId());
            productSku.setSkuName(product.getName()+productSku.getSkuSpec());
            productSku.setSaleNum(0);
            productSku.setStatus(0);
            productSkuMapper.insert(productSku);
        }
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.insert(productDetails);
    }

    @Override
    public Product getProductById(Long id) {
        Product product = productMapper.selectByIdProduct(id);
        LambdaQueryWrapper<ProductSku> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductSku::getProductId,id);
        List<ProductSku> productSkus = productSkuMapper.selectList(wrapper);
        LambdaQueryWrapper<ProductDetails> productDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productDetailsLambdaQueryWrapper.eq(ProductDetails::getProductId,id);
        ProductDetails productDetails = productDetailsMapper.selectOne(productDetailsLambdaQueryWrapper);
        product.setProductSkuList(productSkus);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    @Override
    public void updateByProductId(Product product) {
        productMapper.updateById(product);
        List<ProductSku> skuList = product.getProductSkuList();
        for (ProductSku productSku : skuList) {
         productSkuMapper.updateById(productSku);
        }
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        LambdaQueryWrapper<ProductDetails> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDetails::getProductId,product.getId());
        productDetailsMapper.update(productDetails,wrapper);
    }

    @Override
    public void deleteByProductId(Long id) {
        LambdaQueryWrapper<ProductDetails> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDetails::getProductId,id);
        productDetailsMapper.delete(wrapper);
        LambdaQueryWrapper<ProductSku> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(ProductSku::getProductId,id);
        productSkuMapper.delete(wrapper1);
        LambdaQueryWrapper<Product> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Product::getId,id);
        productMapper.delete(wrapper2);
    }
}
